package com.bys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bys.mapper.TsnMapper;
import com.bys.po.Tsn;
import com.bys.po.TsnExample;
import com.bys.service.TsnService;
import com.bys.util.PageResult;
import com.bys.vo.TsnVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service

public class TsnServiceImpl implements TsnService {
    @Autowired
    private TsnMapper tsnMapper;
    @Override
    public List<Tsn> findAll() {
        List<Tsn> tbspecs = tsnMapper.selectAll();
        tbspecs.forEach(a ->{
            System.out.println(a.getId());
            System.out.println(a.getSpecName());
        });
        return tsnMapper.selectAll();
    }

    @Override
    public PageResult<Tsn> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Tsn> users = this.findAll();
        Page<Tsn> userPage = (Page<Tsn>) users;
        return  new PageResult<>(userPage.getResult(),userPage.getTotal());
    }

    @Override
    public PageResult<Tsn> search(Integer page, Integer pageSize, TsnVo tsnVo) {
        PageHelper.startPage(page,pageSize);
        if (tsnVo != null){
            TsnExample tsnExample = new TsnExample();
            TsnExample.Criteria criteria = tsnExample.createCriteria();
            if(StrUtil.isNotBlank(tsnVo.getSpec_name())){
                criteria.andSpecNameLike("%" + tsnVo.getSpec_name() + "%");
            }
            List<Tsn> tbspecs = tsnMapper.selectByExample(tsnExample);
            Page<Tsn> tbspecPage = (Page<Tsn>) tbspecs;

            return  new PageResult<>(tbspecPage.getResult(),tbspecPage.getTotal());
        }else{
            List<Tsn> tbspecs = this.findAll();
            Page<Tsn> tbspecPage = (Page<Tsn>) tbspecs;
            return  new PageResult<>(tbspecPage.getResult(),tbspecPage.getTotal());}
    }

    @Override
    public boolean save(Tsn tbspec) {
        return tsnMapper.insert(tbspec) > 0;
    }
}
